package com.macro.mall.product.config;


import org.dozer.spring.DozerBeanMapperFactoryBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

@Configuration
public class DozerBeanMapperConfigure {
    private static final Logger logger = LoggerFactory.getLogger(DozerBeanMapperConfigure.class);

    @Bean(name = "dozerBeanMapperFactoryBean")
    public DozerBeanMapperFactoryBean dozerBeanMapperFactoryBean() throws Exception {
        StringBuilder info = new StringBuilder("\n");

        info.append("=========================================================\n");
        info.append("dozer is initializing ...\n");

        try {
            final DozerBeanMapperFactoryBean dozerBeanMapperFactoryBean = new DozerBeanMapperFactoryBean();

            ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            Resource[] resources = resolver.getResources("classpath*:/dozer/*.xml");
            for (Resource resource : resources) {
                info.append("\tdozer config：").append(resource.getURL()).append("\n");
            }
            dozerBeanMapperFactoryBean.setMappingFiles(resources);
            return dozerBeanMapperFactoryBean;
        } finally {
            info.append("=========================================================\n");

            logger.info(info.toString());

        }
    }
}
